package online.westonline.com.westonline.adapter.tag;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.hhl.library.OnInitSelectedPosition;

import java.util.ArrayList;
import java.util.List;

import online.westonline.com.westonline.R;
import online.westonline.com.westonline.entry.Tag;

public class TagAdapter<T> extends BaseAdapter implements OnInitSelectedPosition {

    private final Context mContext;
    private final List<T> mDataList;
    private int postion=-1;
    private boolean selectable;
    public TagAdapter(Context context) {
        this.mContext = context;
        mDataList = new ArrayList<>();
    }

    public void setSelectable(boolean selectable) {
        this.selectable = selectable;
    }

    @Override
    public int getCount() {
        return mDataList.size();
    }

    @Override
    public Object getItem(int position) {
        return mDataList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        View view = LayoutInflater.from(mContext).inflate(R.layout.item_tag, null);

        TextView textView = (TextView) view.findViewById(R.id.textTag);
        Tag t = (Tag) mDataList.get(position);
        textView.setClickable(selectable);
        textView.setText(t.getContent());
        return view;
    }

    public void onlyAddAll(List<T> datas) {
        mDataList.addAll(datas);
        notifyDataSetChanged();
    }

    public void clearAndAddAll(List<T> datas) {
        mDataList.clear();
        onlyAddAll(datas);
    }
    //设置选中
    public void setPostionSelcted(int postion)
    {
        this.postion = postion;
    }
    @Override
    public boolean isSelectedPosition(int position) {
           if (position==postion)
           {
               return true;
           }
        return false;
    }
}
